from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import gettext_lazy as _

from .manager import UserManager


class User(AbstractUser):
    username = None
    email = models.EmailField(_('email address'), unique=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = UserManager()

    class Meta:
        verbose_name = 'Пользователь'
        verbose_name_plural = 'Пользователи'

    def __str__(self):
        return self.email


class News(models.Model):
    heading = models.CharField('Заголовок', max_length=100)
    content = models.TextField('Содержание')
    published = models.DateTimeField('Опубликована', auto_now_add=True)
    updated = models.DateTimeField('Изменена', auto_now=True)
    tags = models.ManyToManyField('Tag', verbose_name='Теги')
    group = models.ForeignKey('Group', on_delete=models.CASCADE, verbose_name='Группа')
    image = models.ImageField('Изображение', upload_to='%Y/%m/%d', null=True, blank=True)
    url = models.SlugField('url', max_length=100)
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='Пользователь')

    class Meta:
        verbose_name = 'Новость'
        verbose_name_plural = 'Новости'

    def __str__(self):
        return self.heading


class Group(models.Model):
    title = models.CharField('Название', max_length=50)

    class Meta:
        verbose_name = 'Группа'
        verbose_name_plural = 'Группы'

    def __str__(self):
        return self.title


class Tag(models.Model):
    title = models.CharField('Название', max_length=50)

    class Meta:
        verbose_name = 'Тег'
        verbose_name_plural = 'Теги'

    def __str__(self):
        return self.title


class Comment(models.Model):
    content = models.TextField('Содержание')
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='Пользователь')
    published = models.DateTimeField('Опубликован', auto_now_add=True)
    updated = models.DateTimeField('Изменен', auto_now=True)
    news = models.ForeignKey(News, on_delete=models.CASCADE, verbose_name='Новость')

    class Meta:
        verbose_name = 'Комментарий'
        verbose_name_plural = 'Комментарии'

    def __str__(self):
        return self.content
